<template>
  <div class="bannerimg">
    <span>{{ spansize }}</span>
    <div class="img-list">
      <div class="img-btn">
        <em>+</em>
        <input
          type="file"
          @change="upload"
          multiple
          accept=".png,.jpg,.jpeg,.webp"
          v-if="multipleCode == false"
        />
        <input
          type="file"
          @change="oneImg"
          accept=".png,.jpg,.jpeg,.webp"
          v-if="multipleCode == true"
        />
      </div>
      <div
        class="img-box"
        v-for="(item, i) in imgList"
        :key="i"
        @mouseover="over(i)"
        @mouseleave="leave"
      >
        <img :src="item" />
        <div class="item-bg" v-if="overid == i">
          <div class="blow-up" @click="blowUp(item)">
            <el-icon><ZoomIn /></el-icon>
          </div>
          <div class="remove" @click="removeImg(i)">
            <el-icon><CircleClose /></el-icon>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- 放大图片版块 -->
  <div class="blowUp-img" v-if="blowUpCode">
    <div class="quit-blowUp" @click="quitBlowUp"></div>
    <div class="img-box">
      <img :src="blowUpImg" alt="" />
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, defineEmits, defineProps, toRef } from "vue";
import { CircleClose, ZoomIn } from "@element-plus/icons-vue";

let imgList = reactive([]); // 添加图片列表
let imgInfoList = reactive([]); // 添加图片信息列表
let overid = ref(-1); // 图片鼠标移动事件
let blowUpImg = ref(""); // 设置放大图片
let blowUpCode = ref(false); // 设置放大图片

let emit = defineEmits(["upload-img"]);
let prop = defineProps(["size", "multiple"]);
let spansize = toRef(prop, "size");
let multipleCode = toRef(prop, "multiple");

// 单图
let oneImg = (e) => {
  let arr = e.target.files[0];
  var reader = new FileReader();
  reader.readAsDataURL(arr);
  reader.onload = () => {
    // 将路径添加到路径列表中
    imgList[0] = reader.result;
    imgInfoList.push(arr);
  };
  emit("upload-img", { imgInfoList });
};

// 封装一个获取图片Bash64路径的函数
let getimg = (i) => {
  var reader = new FileReader();
  reader.readAsDataURL(i);
  reader.onload = () => {
    // 将路径添加到路径列表中
    imgList.push(reader.result);
  };
};
// 上传按钮点击事件
let upload = (e) => {
  let arr = e.target.files;
  for (var i = 0; i < arr.length; i++) {
    getimg(arr[i]);
    imgInfoList.push(arr[i]);
  }
  emit("upload-img", { imgInfoList });
};

// 图片鼠标进入事件
let over = (i) => {
  overid.value = i;
};
// 图片鼠标离开事件
let leave = () => {
  overid.value = -1;
};
// 图片移除按钮点击事件
let removeImg = (i) => {
  imgList.splice(i, 1);
};
// 图片放大按钮点击事件
let blowUp = (i) => {
  blowUpImg.value = i;
  blowUpCode.value = true;
};
// 退出图片放大效果
let quitBlowUp = () => {
  blowUpCode.value = false;
};
</script>

<style lang="scss" scoped>
.bannerimg {
  display: flex;
  align-items: center;
  span {
    font-size: 13px;
    white-space: nowrap;
    width: 70px;
  }
  .img-list {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    width: 56vw;
    .img-btn {
      width: 120px;
      height: 120px;
      overflow: hidden;
      background: url(../assets/dotted.svg) no-repeat;
      background-size: cover;
      display: flex;
      align-items: center;
      justify-content: center;
      position: relative;
      margin: 0.625rem;
      margin-left: 0;
      em {
        font-size: 3.75rem;
        color: #999;
        padding-bottom: 0.3125rem;
      }
      input {
        width: 100%;
        height: 100%;
        opacity: 0;
        position: absolute;
        top: 0;
        left: 0;
      }
    }

    .img-box {
      width: 7.5rem;
      height: 7.5rem;
      overflow: hidden;
      background: url(../assets/dotted.svg) no-repeat;
      background-size: cover;
      margin: 0.625rem;
      margin-left: 0;
      position: relative;
      overflow: hidden;
      img {
        width: 100%;
      }
      .item-bg {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        border-radius: 0.3125rem;
        background-color: rgba($color: #000000, $alpha: 0.2);
        z-index: 10;
        display: flex;
        align-items: center;
        justify-content: center;
        div {
          width: 20px;
          height: 20px;
          font-size: 20px;
          margin: 0 10px;
          cursor: pointer;
        }
      }
    }
  }
}
.blowUp-img {
  width: 100%;
  height: 100%;
  background-color: rgba($color: #000000, $alpha: 0.4);
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  .quit-blowUp {
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
  }
  .img-box {
    width: 26vmax;
    height: 26vmax;
    background-color: #fff;
    border-radius: 1.25rem;
    margin-bottom: 6.25rem;
    overflow: hidden;
    position: relative;
    z-index: 999;
    img {
      width: 100%;
    }
  }
}
</style>
